/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.modeldocument;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import dgbgui.command.Command;
import dgbgui.command.CommandStack;
import dgbgui.command.UpdateCurrentModelCommand;
import dgbgui.modeldocument.uimodels.DGBModelComboModel;

public class ModelDrivenManager implements ActionListener {

    
    /**
     * Update the current model-driven workspace
     * from the selected model
     */
    public void actionPerformed(ActionEvent event) {
        
        // There a combo box and a button. Combo = model changed and button
        // = update the models
        assert((event.getSource() instanceof JComboBox) || (event.getSource() instanceof JButton));
        if (event.getSource() instanceof JComboBox) {
            // Update the current model
            JComboBox comboBox = (JComboBox)event.getSource();
            DGBModelComboModel comboModel = (DGBModelComboModel)comboBox.getModel();
            
            assert(comboModel != null);
            Command command = new UpdateCurrentModelCommand(comboModel.getSelected());
            CommandStack.getInstance().execute(command);
            
        } else {
            // Get the models from the back end
            ModelDocument.getInstance().updateDGBModels();
        }
        
    }
    
    // ----- Singleton -----
    
    static public ModelDrivenManager getInstance() {
        if ( m_instance == null ) {
            m_instance = new ModelDrivenManager();
        }
        return m_instance;
    }
    
    private ModelDrivenManager() {
        // Do nothing
    }
    
    static private ModelDrivenManager m_instance;

}
